package com.renyi.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.model.MybatisService;
import com.renyi.util.JSONUtils;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class MedicineReturn extends BaseController {

	@Autowired
	private MybatisService mybatisService;

	// 患者-获取快递公司列表
	@RequestMapping("getExpressCompanyList")
	public void getExpressCompanyList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getExpressCompanyListSQL", pMap);
			if (data != null) {
				result.setCode(0); // 0：成功
				result.setData(data);
			} else {
				result.setCode(-1);
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-药品订单退货
	@RequestMapping("/returnMedicineOrder")
	public void returnMedicineOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = paramMap.get("orderID") != null ? paramMap.get("orderID").toString() : "";
			String companyID = paramMap.get("companyID") != null ? paramMap.get("companyID").toString() : "";
			String expressNumber = paramMap.get("expressNumber") != null ? paramMap.get("expressNumber").toString()
					: "";
			String telephone = paramMap.get("telephone") != null ? paramMap.get("telephone").toString() : "";
			String memo = paramMap.get("memo") != null ? paramMap.get("memo").toString() : "";
			String imgUrl1 = paramMap.get("imgUrl1") != null ? paramMap.get("imgUrl1").toString() : "";
			String imgUrl2 = paramMap.get("imgUrl2") != null ? paramMap.get("imgUrl2").toString() : "";
			if ("".equals(telephone) || "".equals(orderID) || "".equals(companyID) || "".equals(expressNumber)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);
			pMap.put("companyID", companyID);
			pMap.put("expressNumber", expressNumber);
			pMap.put("telephone", telephone);
			pMap.put("memo", memo);
			pMap.put("imgUrl1", imgUrl1);
			pMap.put("imgUrl2", imgUrl2);
			int key = (Integer) this.mybatisService.insertIbatisObject("returnMedicineOrderSQL", pMap);
			if (key > 0) {
				pMap.put("status", 6);//1-待付款 2-待发货 3-待收货 4-待评价 5-已评价 6-待退货 7-已退货 8-已作废
				key = (Integer) this.mybatisService.updateIbatisObject("setMedicineOrderStatusSQL", pMap);
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-获取药品订单退货信息
	@RequestMapping("getReturnMedicineOrder")
	public void getReturnMedicineOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = paramMap.get("orderID") != null ? paramMap.get("orderID").toString() : "";
			if ("".equals(orderID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getReturnMedicineOrderSQL", pMap);
			if (data != null) {
				result.setCode(0); // 0：成功
				result.setData(data);
			} else {
				result.setCode(-1);
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}
}
